/**
 * 命令管理器
 * 统一管理所有命令类型，处理命令的分发和注册
 */
import ElectricalDataCommand from './commands/electricalDataCommand';
import SwitchStateCommand from './commands/switchStateCommand';
import QuerySwitchStateCommand from './commands/querySwitchStateCommand';
import QueryEnergyCountCommand from './commands/queryEnergyCountCommand';
import QueryEnergyRecordCommand from './commands/queryEnergyRecordCommand';
import QueryEnergyDateCommand from './commands/queryEnergyDateCommand';
import FactoryResetCommand from './commands/factoryResetCommand';
import AddTimerCommand from './commands/addTimerCommand';
import DeleteTimerCommand from './commands/deleteTimerCommand';
import QueryTimerCommand from './commands/queryTimerCommand';
import UpdateTimerCommand from './commands/updateTimerCommand';
import ChildLockCommand from './commands/childLockCommand';
import QueryChildLockCommand from './commands/queryChildLockCommand';
import LEDStateCommand from './commands/ledStateCommand';
import QueryLEDStateCommand from './commands/queryLedStateCommand';
import PowerOnStateCommand from './commands/powerOnStateCommand';
import QueryPowerOnStateCommand from './commands/queryPowerOnStateCommand';
import QueryConfigCommand from './commands/queryConfigCommand';
import SetConfigCommand from './commands/setConfigCommand';
import QueryDeviceInfoCommand from './commands/queryDeviceInfoCommand';
import OTAUpdateCommand from './commands/otaUpdateCommand';

class CommandManager {
  // 注册所有命令类型
  static Commands = {
    ELECTRICAL_DATA: ElectricalDataCommand,
    SWITCH_STATE: SwitchStateCommand,
    QUERY_SWITCH_STATE: QuerySwitchStateCommand,
    QUERY_ENERGY_COUNT: QueryEnergyCountCommand,
    QUERY_ENERGY_RECORD: QueryEnergyRecordCommand,
    QUERY_ENERGY_DATE: QueryEnergyDateCommand,
    FACTORY_RESET: FactoryResetCommand,
    ADD_TIMER: AddTimerCommand,
    DELETE_TIMER: DeleteTimerCommand,
    QUERY_TIMER: QueryTimerCommand,
    UPDATE_TIMER: UpdateTimerCommand,
    CHILD_LOCK: ChildLockCommand,
    QUERY_CHILD_LOCK: QueryChildLockCommand,
    LED_STATE: LEDStateCommand,
    QUERY_LED_STATE: QueryLEDStateCommand,
    POWER_ON_STATE: PowerOnStateCommand,
    QUERY_POWER_ON_STATE: QueryPowerOnStateCommand,
    QUERY_CONFIG: QueryConfigCommand,
    SET_CONFIG: SetConfigCommand,
    QUERY_DEVICE_INFO: QueryDeviceInfoCommand,
    OTA_UPDATE: OTAUpdateCommand
  };

  /**
   * 解析响应帧
   * @param {Uint8Array} frame - 接收到的数据帧
   * @returns {Object|null} 解析结果
   */
  static parseFrame(frame) {
    console.log('[CMD管理器] 开始解析数据帧');

    // 获取命令类型
    if (frame.length < 5) return null;
    const cmdType = frame[4];

    // 根据命令类型选择对应的命令处理器
    let handler = null;
    for (const [name, cmd] of Object.entries(this.Commands)) {
      if (cmd.CMD === cmdType) {
        console.log('[CMD管理器] 找到命令处理器:', name);
        handler = cmd;
        break;
      }
    }

    if (!handler) {
      console.warn('[CMD管理器] 未找到命令处理器:', `0x${cmdType.toString(16)}`);
      return null;
    }

    // 使用对应的处理器解析数据
    const result = handler.parseResponse(frame);
    if (result) {
      return {
        cmd: cmdType,
        data: result
      };
    }

    return null;
  }

  /**
   * 创建命令
   * @param {string} commandType - 命令类型名称
   * @param {Object} params - 命令参数
   * @returns {Uint8Array|null} 命令帧
   */
  static createCommand(commandType, params = null) {
    console.log('[CMD管理器] 创建命令:', commandType, params);

    const handler = this.Commands[commandType];
    if (!handler) {
      console.warn('[CMD管理器] 未知的命令类型:', commandType);
      return null;
    }

    // 调用 createCommand 或 createQuery 方法
    const createMethod = handler.createCommand || handler.createQuery;
    if (!createMethod) {
      console.warn('[CMD管理器] 命令处理器没有创建方法');
      return null;
    }

    return createMethod.call(handler, params);
  }

  /**
   * 创建恢复出厂设置命令
   */
  static createFactoryResetCommand() {
    return this.createCommand('FACTORY_RESET');
  }

  /**
   * 创建设置儿童安全锁命令
   * @param {boolean} state - 锁定状态
   */
  static createSetChildLockCommand(state) {
    return ChildLockCommand.createCommand(state);
  }

  /**
   * 创建查询儿童安全锁状态命令
   */
  static createQueryChildLockCommand() {
    return this.createCommand('QUERY_CHILD_LOCK');
  }

  /**
   * 创建设置LED状态命令
   * @param {boolean} state - LED开关状态
   */
  static createSetLEDStateCommand(state) {
    return LEDStateCommand.createCommand(state);
  }

  /**
   * 创建查询LED状态命令
   */
  static createQueryLEDStateCommand() {
    return this.createCommand('QUERY_LED_STATE');
  }

  /**
   * 创建设置断电后通电状态命令
   * @param {number} state - 通电状态(0:记忆, 1:开启, 2:关闭)
   */
  static createSetPowerOnStateCommand(state) {
    return PowerOnStateCommand.createCommand(state);
  }

  /**
   * 创建查询断电后通电状态命令
   */
  static createQueryPowerOnStateCommand() {
    return this.createCommand('QUERY_POWER_ON_STATE');
  }

  /**
   * 创建查询配置命令
   */
  static createQueryConfigCommand() {
    return this.createCommand('QUERY_CONFIG');
  }

  /**
   * 创建设置配置命令
   * @param {Object} config - 配置对象
   */
  static createSetConfigCommand(config) {
    return this.createCommand('SET_CONFIG', config);
  }

  /**
   * 创建查询设备信息命令
   */
  static createQueryDeviceInfoCommand() {
    return this.createCommand('QUERY_DEVICE_INFO');
  }

  /**
   * 创建OTA升级命令
   * @param {string} otaUrl - OTA升级固件URL
   */
  static createOTAUpdateCommand(otaUrl) {
    return OTAUpdateCommand.createCommand(otaUrl);
  }
}

export default CommandManager; 